Skip to content

Conversation

@frankie567
Copy link
Contributor

Description

The Dramatiq middleware was missing the after_skip_message hook, which is triggered in place of after_process_message when SkipMessage is raised.

Thus, the isolated scope and transaction that are opened in before_process_message were never closed; leading to a memory leak particularly visible in a context where lot of messages are skipped.

The fix is simply to assign after_skip_message to after_process_message, so the teardown logic is triggered. This pattern is used in the Dramatiq code base.

Issues

@frankie567 frankie567 requested a review from a team as a code owner January 20, 2026 13:33
@github-actions
Copy link
Contributor

github-actions bot commented Jan 20, 2026

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


Bug Fixes 🐛

  • fix(dramatiq): cleanup isolated scope and transaction when message is skipped by frankie567 in #5346

🤖 This preview updates automatically when you update the PR.

… skipped

The Dramatiq middleware was missing the [`after_skip_message` hook](https://dramatiq.io/reference.html#dramatiq.Middleware.after_skip_message),
which is triggered in place of [`after_process_message`](https://dramatiq.io/reference.html#dramatiq.Middleware.after_skip_message)
when [`SkipMessage`](https://dramatiq.io/reference.html#dramatiq.middleware.SkipMessage) is raised.

Thus, the isolated scope and transaction that are opened in [`before_process_message`](https://dramatiq.io/reference.html#dramatiq.Middleware.before_process_message) were never closed; leading to a memory leak particularly visible in a context where lot of messages are skipped.

The fix is simply to assign `after_skip_message` to `after_process_message`, so the teardown logic is triggered. This pattern is used in the [Dramatiq code base](https://github.com/Bogdanp/dramatiq/blob/143aaa228521606806a87daefff2d21f88607e70/dramatiq/middleware/time_limit.py#L97).
@frankie567 frankie567 force-pushed the dramatiq-skip-message branch from d3bbb38 to 2ebc4e2 Compare January 20, 2026 13:34
Copy link
Contributor

@sentrivana sentrivana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @frankie567, looks good to me.

@sentrivana sentrivana enabled auto-merge (squash) January 29, 2026 09:57
@codecov
Copy link

codecov bot commented Jan 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.23%. Comparing base (2ce4379) to head (2ebc4e2).
⚠️ Report is 116 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5346      +/-   ##
==========================================
- Coverage   84.25%   84.23%   -0.03%     
==========================================
  Files         181      183       +2     
  Lines       18463    19148     +685     
  Branches     3288     3456     +168     
==========================================
+ Hits        15556    16129     +573     
- Misses       1894     1970      +76     
- Partials     1013     1049      +36     
Files with missing lines Coverage Δ
sentry_sdk/integrations/dramatiq.py 84.54% <100.00%> (+0.14%) ⬆️

... and 33 files with indirect coverage changes

@sentrivana sentrivana merged commit ae8f065 into getsentry:master Jan 29, 2026
156 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants